package com.luke.nio.channel;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * MappedByteBuffer 可让文件直接在内存（堆外内存）修改，操作系统不需要拷贝一次
 * @Descrtption MappedByteBufferTest
 * @Author luke
 * @Date 2020/4/23
 **/
public class MappedByteBufferTest {

    public static void main(String[] args) throws Exception{
        RandomAccessFile randomAccessFile =
                new RandomAccessFile("E:\\netty\\a1.txt","rw");
        FileChannel fileChannel = randomAccessFile.getChannel();

        MappedByteBuffer mappedByteBuffer =
                fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 5);//可修改范围

        mappedByteBuffer.put(0,(byte)'H');
        mappedByteBuffer.put(1,(byte)'X');
        //mappedByteBuffer.put(5,'W'); //IndexOutOfBoundsException

        randomAccessFile.close();
    }

}
